#include "mainpage.h"
#include "ui_mainpage.h"
#include "mydata.h"
#include "netmain.h"
#include <QTimer>

MainPage::MainPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MainPage),
    sendTimer(new QTimer),
    timer(new QTimer),
    timer2(new QTimer),
    timer3(new QTimer),
    timer4(new QTimer)

{
    ui->setupUi(this);
    connect(sendTimer,&QTimer::timeout,
    this, &MainPage::sendTimerTimeoutSlot);
    connect(NetMain::getObject(),&NetMain::newCarPos,
            this, &MainPage::newCarPosSlot);
    connect(NetMain::getObject(),&NetMain::newLampColor,
            this, &MainPage::newLampColorSlot);
    connect(timer, &QTimer::timeout,
            this, &MainPage::getcarmilageSlot);
    connect(timer2, &QTimer::timeout,
            this, &MainPage::getcarmilageSlot2);
    connect(timer3, &QTimer::timeout,
            this, &MainPage::getcarbatterySlot);
    connect(timer2, &QTimer::timeout,
            this, &MainPage::getcarbatterySlot2);
}

MainPage::~MainPage()
{
    delete ui;
}

void MainPage::on_btnShowAll_clicked()
{
    ui->swMain->setCurrentIndex(0);
}

void MainPage::on_btnShowCar_clicked()
{
    ui->swMain->setCurrentIndex(1);
}

void MainPage::on_btnShowFlow_clicked()
{
    ui->swMain->setCurrentIndex(2);
}

void MainPage::on_btnShowRound_clicked()
{
    ui->swMain->setCurrentIndex(3);
}

void MainPage::on_btnShowCarControl_clicked()
{
    ui->swMain->setCurrentIndex(4);
}

void MainPage::on_btnShowConfig_clicked()
{
    ui->swMain->setCurrentIndex(5);
}

void MainPage::on_btnShowInfo_clicked()
{
    ui->swMain->setCurrentIndex(6);
}


void MainPage::sendTimerTimeoutSlot()
{
        if(a>9)
         {
            b++;
            a = 0;

            if(b == 1)
            {
                c = 6;
            }
            else if(b == 2)
            {
                c = 10;
            }
            else if(b == 3)
            {
                c = 5;
            }

            else if(b == 4)
            {
                c = 13;
            }
            else if(b == 5)
            {
                c = 9;
            }
            else if(b == 6)
            {
                c = 0;
                b = 0;
            }

         }
     MyData::getObject()->updateCarPos(0,c,a);
     if(ui->swMain->currentIndex()==0)
         ui->page->update();
     if(ui->swMain->currentIndex()==1)
         ui->page_2->update();
     if(ui->swMain->currentIndex()==2)
         ui->page_3->update();
     a++;
}

void MainPage::newCarPosSlot(int carId, int roundId, int roundPos)
{
    MyData::getObject()->updateCarPos(carId,roundId,roundPos);
    if(ui->swMain->currentIndex()==0)
        ui->page->update();
    if(ui->swMain->currentIndex()==1)
        ui->page_2->update();
    if(ui->swMain->currentIndex()==2)
        ui->page_3->update();
}

void MainPage::on_btnAlarmOpen_clicked()
{
    NetMain::getObject()->setCarAlarm(0,true);
}

void MainPage::on_btnAlarmClose_clicked()
{
    NetMain::getObject()->setCarAlarm(0,false);
}

void MainPage::on_btnLightOpen_clicked()
{
    NetMain::getObject()->setCarLight(0,true);
}

void MainPage::on_btnLightClose_clicked()
{
    NetMain::getObject()->setCarLight(0,false);
}

void MainPage::on_btnAlarmLightOpen_clicked()
{
    NetMain::getObject()->setCarAlarmLight(0,true);
}

void MainPage::on_btnAlarmLightClose_clicked()
{
    NetMain::getObject()->setCarAlarmLight(0,false);
}

void MainPage::on_btnCarStop_clicked()
{
    ui->lbStatus->setText("停止");
    NetMain::getObject()->setCarStatus(0,false);
    timer->stop();
    timer3->stop();

}

void MainPage::on_btnCarStart_clicked()
{

    ui->lbStatus->setText("启动");
    NetMain::getObject()->setCarStatus(0,true);
    ui->lbBattery->setNum(100);
    timer->start(300);
    timer3->start(1000);

}

void MainPage::on_btnAlarmOpen_2_clicked()
{
    NetMain::getObject()->setCarAlarm(1,true);
}

void MainPage::on_btnAlarmClose_2_clicked()
{
    NetMain::getObject()->setCarAlarm(1,false);
}

void MainPage::on_btnLightOpen_2_clicked()
{
    NetMain::getObject()->setCarLight(1,true);
}

void MainPage::on_btnLightClose_2_clicked()
{
    NetMain::getObject()->setCarLight(1,false);
}

void MainPage::on_btnAlarmLightOpen_2_clicked()
{
    NetMain::getObject()->setCarAlarmLight(1,true);
}

void MainPage::on_btnAlarmLightClose_2_clicked()
{
    NetMain::getObject()->setCarAlarmLight(1,false);
}

void MainPage::on_btnCarStart_2_clicked()
{
    ui->lbStatus_2->setText("启动");
    NetMain::getObject()->setCarStatus(1,true);
    ui->lbBattery_2->setNum(80);
    timer2->start(300);
    timer4->start(1000);

}

void MainPage::on_btnCarStop_2_clicked()
{
    ui->lbStatus_2->setText("停止");
    NetMain::getObject()->setCarStatus(1,false);
    timer2->stop();
    timer4->stop();
}

void MainPage::newLampColorSlot(int junctionId, int lampId, int color)
{
    MyData::getObject()->updatenewLampColorSlot(junctionId,lampId,color);
    if(ui->swMain->currentIndex() == 0)
        ui->page->update();
    if(ui->swMain->currentIndex() == 3)
        ui->page_4->update();
}

void MainPage::on_btnJunNor_clicked()
{
    NetMain::getObject()->setJunNode(0,0);
}

void MainPage::on_btnJunStop_clicked()
{
    NetMain::getObject()->setJunNode(0,1);
}

void MainPage::on_btnJunNight_clicked()
{
    NetMain::getObject()->setJunNode(0,2);
}

void MainPage::on_btnJunHor_clicked()
{
    NetMain::getObject()->setJunNode(0,3);
}

void MainPage::on_btnJunVor_clicked()
{
    NetMain::getObject()->setJunNode(0,4);
}

void MainPage::getcarmilageSlot()
{
    ui->lbMileage->setNum(ui->lbMileage->text().toInt()+1);
}

void MainPage::getcarmilageSlot2()
{
    ui->lbMileage_2->setNum(ui->lbMileage_2->text().toInt()+1);
}

void MainPage::getcarbatterySlot()
{
    ui->lbBattery->setNum(ui->lbBattery->text().toDouble()-0.5);
}

void MainPage::getcarbatterySlot2()
{
     ui->lbBattery_2->setNum(ui->lbBattery_2->text().toDouble()-0.5);
}

